package com.example.smart.adapter.home

import android.app.Activity
import android.view.LayoutInflater
import android.view.ViewGroup
import com.example.lib.extend.setOnDoubleClickListener
import com.example.lib.utils.sp.SpUtils
import com.example.smart.R
import com.example.smart.base.adapter.BaseRecyclerAdapter
import com.example.smart.base.adapter.BaseViewHolder
import com.example.smart.data.PreventionData
import com.example.smart.databinding.ItemPreventionBinding
import com.example.smart.utils.DisplayUtils

class PreventionAdapter(val activity: Activity) : BaseRecyclerAdapter<ItemPreventionBinding, PreventionData>(activity) {
    private var flag1 = SpUtils.getBoolean("home1", false)
    private var flag2 = SpUtils.getBoolean("home2", false)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): BaseViewHolder<ItemPreventionBinding> {
        return BaseViewHolder(ItemPreventionBinding.bind(LayoutInflater.from(context)
            .inflate(R.layout.item_prevention, parent, false)))
    }

    override fun onBindView(itemView: ItemPreventionBinding, bean: PreventionData, position: Int) {
        itemView.ItemImage.setImageResource(bean.image)
        itemView.ItemTitle.text = bean.title
        itemView.ItemMessage.text = bean.message
        itemView.root.setOnDoubleClickListener {
            DisplayUtils.toPreventionDetails(activity)
        }
        if (position == 0) {
            itemView.Gz.setImageResource(if (flag1) R.mipmap.icon_heart else R.mipmap.icon_heart_not)

            itemView.GzBack.setOnDoubleClickListener {
                flag1 = !flag1
                itemView.Gz.setImageResource(if (flag1) R.mipmap.icon_heart else R.mipmap.icon_heart_not)
                itemView.GzBack.setBackgroundResource(if (flag1) com.example.lib.R.drawable.round_white else R.mipmap.icon_heart_not_back)
                SpUtils.putBoolean("home1", flag1)
            }

            itemView.Gz.setOnDoubleClickListener {
                flag1 = !flag1
                itemView.Gz.setImageResource(if (flag1) R.mipmap.icon_heart else R.mipmap.icon_heart_not)
                itemView.GzBack.setBackgroundResource(if (flag1) com.example.lib.R.drawable.round_white else R.mipmap.icon_heart_not_back)
                SpUtils.putBoolean("home1", flag1)
            }
        } else {
            itemView.Gz.setImageResource(if (flag2) R.mipmap.icon_heart else R.mipmap.icon_heart_not)

            itemView.GzBack.setOnDoubleClickListener {
                flag2 = !flag2
                itemView.Gz.setImageResource(if (flag2) R.mipmap.icon_heart else R.mipmap.icon_heart_not)
                itemView.GzBack.setBackgroundResource(if (flag2) com.example.lib.R.drawable.round_white else R.mipmap.icon_heart_not_back)
                SpUtils.putBoolean("home2", flag2)
            }

            itemView.Gz.setOnDoubleClickListener {
                flag2 = !flag2
                itemView.Gz.setImageResource(if (flag2) R.mipmap.icon_heart else R.mipmap.icon_heart_not)
                itemView.GzBack.setBackgroundResource(if (flag2) com.example.lib.R.drawable.round_white else R.mipmap.icon_heart_not_back)
                SpUtils.putBoolean("home2", flag2)
            }
        }

    }
}